package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.indexService.WayBillIndexService;
import cn.itcast.bos.service.take_delivery.WayBillService;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.utils.FileUtils;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xbqco on 2018/7/20.
 */
@Controller
public class WayBillController {

    @Autowired
    private WayBillService wayBillService;

    @Autowired
    private WayBillIndexService wayBillIndexService;

    @RequestMapping("/waybill_pageQuery")
    @ResponseBody
    public Map<String ,Object> findPage(int page, int rows,WayBill wayBill){
        Pageable pageable = new PageRequest(page-1,rows);
       Page<WayBill> p= wayBillService.findPage(wayBill,pageable);
       Map map = new HashMap();
       map.put("total",p.getTotalElements());
       map.put("rows",p.getContent());
       return map;

    }

    @RequestMapping("waybill_save")
    @ResponseBody
    public String save(WayBill wayBill){

        wayBillIndexService.save(wayBill);
        return wayBillService.save(wayBill);
    }

    @RequestMapping("wayBill_findOneByWayBillNum")
    @ResponseBody
    public Map findOne(String wayBillNum){
        WayBill wayBill=wayBillService.findByWayBillNUm(wayBillNum);
        Map map = new HashMap();
        map.put("success","success");
        map.put("wayBill",wayBill);
        return map;
    }
    @RequestMapping("wayBill_saveAsXls")
    public String saveAsXls( WayBill wayBill, HttpServletRequest request, HttpServletResponse response){
        List<WayBill> list = new ArrayList<>();
            try {
                list = wayBillService.findPage(wayBill);
            }catch (Exception e){
                e.printStackTrace();
                return "redirect:unauthorized.html";
            }



        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet  sheet=workbook.createSheet();
        System.out.println(wayBill);
        Row row0=sheet.createRow(0);
        row0.createCell(0).setCellValue("重量");
        row0.createCell(1).setCellValue("运单编号");
        row0.createCell(2).setCellValue("寄件人");
        row0.createCell(3).setCellValue("寄件人电话");
        row0.createCell(4).setCellValue("寄件人公司");
        row0.createCell(5).setCellValue("寄件人详细地址");
        row0.createCell(6).setCellValue("收件人");
        row0.createCell(7).setCellValue("收件人电话");
        row0.createCell(8).setCellValue("收件人公司");
        row0.createCell(9).setCellValue("收件人详细地址");
        row0.createCell(10).setCellValue("产品类型");
        row0.createCell(11).setCellValue("支付类型");

        for(WayBill wayBill1:list){
            Row row1=sheet.createRow(sheet.getLastRowNum()+1);
            row1.createCell(0).setCellValue(wayBill1.getWeight());
            row1.createCell(1).setCellValue(wayBill1.getWayBillNum());
            row1.createCell(2).setCellValue(wayBill1.getSendName());
            row1.createCell(3).setCellValue(wayBill1.getSendMobile());
            row1.createCell(4).setCellValue(wayBill1.getSendCompany());
            row1.createCell(5).setCellValue(wayBill1.getSendAddress());
            row1.createCell(6).setCellValue(wayBill1.getRecName());
            row1.createCell(7).setCellValue(wayBill1.getRecMobile());
            row1.createCell(8).setCellValue(wayBill1.getRecCompany());
            row1.createCell(9).setCellValue(wayBill1.getRecAddress());
            row1.createCell(10).setCellValue(wayBill1.getGoodsType());
            row1.createCell(11).setCellValue(wayBill1.getPayTypeNum());

        }

        response.setContentType("application/vnd.ms-excel");
        String fileName = "运单数据.xls";
        String agent=request.getHeader("user-agent");
        try {
            fileName = FileUtils.encodeDownloadFilename(fileName, agent);
            response.setHeader("Content-Disposition","attachment;filename=" + fileName);

            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            workbook.close();
        } catch (IOException e){
            e.printStackTrace();
        }
    return "redirect:/pages/take_delivery/waybill_manage.html";
    }

    @RequestMapping("saveAsPdf")

    public String saveAsPdf(WayBill wayBill,HttpServletRequest request,HttpServletResponse response) throws Exception {
        List<WayBill> wayBills = new ArrayList<>();
        try {
            wayBills = wayBillService.findPage(wayBill);
        }catch (Exception e){
            e.printStackTrace();
            return "redirect:unauthorized.html";
        }

        response.setContentType("application/pdf");
        String name = "运单数据表.pdf";
        String agent = request.getHeader("user-agent");
        name = FileUtils.encodeDownloadFilename(name, agent);
        response.setHeader("Content-Disposition","attachment;filename="+name);
        String wayBillJasper = request.getSession().getServletContext().getRealPath("WEB-INF/jasper/report2.jrxml");
        JasperReport report = JasperCompileManager.compileReport(wayBillJasper);
        Map<String,Object> map= new HashMap();
        map.put("company","联邦快递");
        map.put("sAdress",wayBill.getSendAddress());
        map.put("rAddress",wayBill.getRecAddress());
        map.put("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        JasperPrint jasperPrint = JasperFillManager.fillReport(report,map,new JRBeanCollectionDataSource(wayBills));
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT,jasperPrint);
        exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,response.getOutputStream());
        exporter.exportReport();
        response.getOutputStream().close();

        return "redirect:/pages/take_delivery/waybill_manage.html";
    }


}
